// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Data;
using JetBrains.Annotations;

namespace SnapObjects.Data.Sqlite
{
    public class SqliteByteArrayTypeMapping : ByteArrayTypeMapping
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="SqliteGuiSqliteULongTypeMappingdTypeMapping" /> class.
        /// </summary>
        /// <param name="dbTypeName"> The name of the database type. </param>
        /// <param name="dbType"> The <see cref="DbType" /> to be used. </param>
        public SqliteByteArrayTypeMapping(
            [NotNull] string dbTypeName,
            [CanBeNull] DbType? dbType = null)
            : base(dbTypeName, dbType)
        {
        }

        /// <summary>
        ///     Creates a copy of this mapping.
        /// </summary>
        /// <param name="dbTypeName"> The name of the database type. </param>
        /// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param>
        /// <returns> The newly created mapping. </returns>
        public override TypeMapping Clone(string dbTypeName, int? size)
        {
            return new SqliteByteArrayTypeMapping(
                           dbTypeName, this.DbType);
        }

        protected override bool IsAssignable(Type modelType, Type dbType)
        {
            // Workaround Microsoft.Data.Sqlite.SqliteDataReader.GetFieldType() issue
            // when the column's value is null in the first record, 
            // the column's dbtype is always int32.
            // so ignore the type mathcing.
            return true;
        }

        public override bool IsStrictAssignable(Type modelType)
        {
            return true;
        }

    }
}
